﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.PointCloud.CUiPOCInsertPointCloudDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.Options;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.PointCloud;

public class CUiPOCInsertPointCloudDlg : TxForm
{
  private const string LAST_BROWSE_LOCATION = "LAST_BROWSE_LOCATION";
  private string _lastBrowseLocation;

  public List<string> PodFiles { set; get; }

  public CUiPOCInsertPointCloudDlg()
  {
    this.PodFiles = new List<string>();
    ((Control) this).Name = nameof (CUiPOCInsertPointCloudDlg);
    this.LoadSettings();
  }

  private void LoadSettings()
  {
    this._lastBrowseLocation = TxOptionsEx.PointCloudRoot;
    object obj;
    TxFormSettings.RestoreFormValue((Form) this, "LAST_BROWSE_LOCATION", ref obj);
    if (obj != null)
    {
      try
      {
        this._lastBrowseLocation = Convert.ToString(obj);
      }
      catch (Exception ex)
      {
        TxApplication.LogWriter.WriteExceptionLine(ex);
      }
    }
    if (this.IsSubfolder(TxOptionsEx.PointCloudRoot, this._lastBrowseLocation))
      return;
    this._lastBrowseLocation = TxOptionsEx.PointCloudRoot;
  }

  public void ShowDialog()
  {
    if (!this.IsPointCloudRootValid())
    {
      int num1 = (int) TxMessageBox.Show(PointCloudStringTable.POINT_CLOUD_ROOT_DONT_EXISTS, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    else
    {
      if (TxGraphicViewerEx.The.ActiveSectionVolumes > 1)
      {
        int num2 = (int) TxMessageBox.Show(PointCloudStringTable.POINT_CLOUD_MORE_THAN_ONE_SECTION_VOLUME, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
      }
      using (OpenFileDialog openFileDialog = new OpenFileDialog())
      {
        openFileDialog.Filter = PointCloudStringTable.OPEN_DIALOG_FILE_FILTER;
        openFileDialog.Title = PointCloudStringTable.POINT_CLOUD_SELECT_FILE_TITLE;
        openFileDialog.InitialDirectory = this._lastBrowseLocation;
        openFileDialog.Multiselect = true;
        if (openFileDialog.ShowDialog() != DialogResult.OK || ((IEnumerable<string>) openFileDialog.FileNames).Count<string>() <= 0)
          return;
        if (this.IsFileUnderPointCloudRoot(openFileDialog.FileNames[0]))
        {
          this.PodFiles = ((IEnumerable<string>) openFileDialog.FileNames).ToList<string>();
          this.InsertPointClouds();
        }
        else
        {
          int num3 = (int) TxMessageBox.Show(PointCloudStringTable.SELECTED_FILE_IS_NOT_IN_POINT_CLOUD_ROOT, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
      }
    }
  }

  private ErrorCode InsertPointCloud(string podFullPath)
  {
    ErrorCode errorCode = ErrorCode.Success;
    try
    {
      if (this.IsFileUnderPointCloudRoot(podFullPath))
      {
        string relativePath = this.GetRelativePath(TxOptionsEx.PointCloudRoot, podFullPath);
        TxPhysicalRootEx txPhysicalRootEx = new TxPhysicalRootEx();
        if (!txPhysicalRootEx.DoesPointCloudExist(relativePath, TxOptionsEx.PointCloudRoot))
        {
          TxPointCloudCreationDataEx cloudCreationDataEx = new TxPointCloudCreationDataEx(TxApplication.ActiveDocument.PhysicalRoot.GetUniqueDisplayName(Path.GetFileNameWithoutExtension(podFullPath)), podFullPath, relativePath);
          txPhysicalRootEx.CreatePointCloud(cloudCreationDataEx);
        }
        else
          errorCode = ErrorCode.ErrorFileAlreadyExists;
      }
      else
        errorCode = ErrorCode.ErrorFileNotUnderPointCloudRoot;
    }
    catch (TxPointCloudCreationFailedException ex)
    {
      int num = (int) TxMessageBox.Show(PointCloudStringTable.POINT_CLOUD_FAILED_TO_CREATE, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    catch (TxPointCloudNotSupportedException ex)
    {
      int num = (int) TxMessageBox.Show(PointCloudStringTable.POINT_CLOUD_NOT_SUPPORTED, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    return errorCode;
  }

  public void InsertPointClouds()
  {
    List<string> stringList = new List<string>();
    foreach (string podFile in this.PodFiles)
    {
      if (this.InsertPointCloud(podFile) == ErrorCode.ErrorFileAlreadyExists)
        stringList.Add(Path.GetFileName(podFile));
    }
    string str = string.Empty;
    if (stringList.Count == 1)
      str = string.Format(PointCloudStringTable.POINT_CLOUD_FILE_EXIST, (object) stringList[0]);
    else if (stringList.Count > 1)
      str = string.Format(PointCloudStringTable.POINT_CLOUD_FILES_EXIST, (object) $"{Environment.NewLine}- {string.Join(Environment.NewLine + "- ", stringList.ToArray())}");
    if (this.CheckForInternalErrors("API_GRAPHICS_POINTCLOUD_FILE_WRONG_TYPE"))
    {
      if (!string.IsNullOrEmpty(str))
        str += Environment.NewLine;
      str += PointCloudStringTable.POINT_CLOUD_FILE_WRONG_TYPE;
    }
    if (this.CheckForInternalErrors("API_GRAPHICS_POINTCLOUD_FILE_INVALID_NAME"))
    {
      if (!string.IsNullOrEmpty(str))
        str += Environment.NewLine;
      str += PointCloudStringTable.POINT_CLOUD_FILE_INVALID_NAME;
    }
    if (!string.IsNullOrEmpty(str))
    {
      int num = (int) TxMessageBox.Show(str, PointCloudStringTable.CMD_POINT_CLOUD_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    TxApplication.RefreshDisplay();
    this.SaveSettings();
  }

  private bool CheckForInternalErrors(string errCode)
  {
    bool flag = false;
    foreach (IList<string> allParameter in TxErrorStack.GetAllParameters(errCode))
    {
      if (this.PodFiles.Contains(allParameter[0]))
      {
        flag = true;
        break;
      }
    }
    return flag;
  }

  private void SaveSettings()
  {
    if (this.PodFiles.Count > 0)
      this._lastBrowseLocation = Path.GetDirectoryName(this.PodFiles[0]);
    TxFormSettings.StoreFormValue((Form) this, "LAST_BROWSE_LOCATION", (object) this._lastBrowseLocation);
  }

  private bool IsPointCloudRootValid()
  {
    bool flag = false;
    if (Path.IsPathRooted(TxOptionsEx.PointCloudRoot))
      flag = new DirectoryInfo(TxOptionsEx.PointCloudRoot).Exists;
    return flag;
  }

  private bool IsFileUnderPointCloudRoot(string fileName)
  {
    bool flag = false;
    if (fileName != string.Empty)
      flag = this.IsSubfolder(TxOptionsEx.PointCloudRoot, new FileInfo(fileName).Directory.FullName);
    return flag;
  }

  private bool IsSubfolder(string rootFolder, string subFolder)
  {
    rootFolder = rootFolder.TrimBackSlash();
    bool flag = false;
    if (Path.IsPathRooted(rootFolder) && Path.IsPathRooted(subFolder))
    {
      DirectoryInfo directoryInfo = new DirectoryInfo(rootFolder);
      flag = new DirectoryInfo(subFolder).FullName.ToLower().StartsWith(directoryInfo.FullName.ToLower());
    }
    return flag;
  }

  private string GetRelativePath(string rootFolder, string fullPath)
  {
    string relativePath = fullPath;
    if (this.IsSubfolder(rootFolder, fullPath))
      relativePath = relativePath.Remove(0, rootFolder.Length);
    return relativePath;
  }

  private void InitializeComponent()
  {
    ((Control) this).SuspendLayout();
    ((ContainerControl) this).AutoScaleDimensions = new SizeF(8f, 16f);
    ((Form) this).ClientSize = new Size(292, 273);
    ((Control) this).Name = nameof (CUiPOCInsertPointCloudDlg);
    ((Form) this).Load += new EventHandler(this.CUiPOCInsertPointCloudDlg_Load);
    ((Control) this).ResumeLayout(false);
  }

  private void CUiPOCInsertPointCloudDlg_Load(object sender, EventArgs e)
  {
  }
}
